package longrise.phone.com.bjjt_henan.compensate;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.Constant.URLConstant;
import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.Utils.UiUtils;

/**
 * 阅读是否符合快撤快赔的条件温馨提示，不符合提示报警，Created by Administrator on 2016/5/12.
 */

public class PromptView extends LFView implements View.OnClickListener {
    private Context context = null;
    private View view = null;
    private Button bt_prompt;
    private Button bt_prompt_callpolice;
    private BaseApplication mApplication;
    private Dialog processDialog;
    private LinearLayout hn_btn_back;
    private TextView tv_title;
    private ImageView tipsTV;

    public PromptView(Context c) {
        super(c);
        this.context = c;
        this.mApplication = ((MainActivity) c).getAppData();
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_prompt, null);
        bt_prompt = (Button) view.findViewById(R.id.bt_prompt);
        bt_prompt_callpolice = (Button) view.findViewById(R.id.bt_prompt_callpolice);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("温馨提示");
        tipsTV = (ImageView) view.findViewById(R.id.promt_tv_tips2);
        setViewSize();
    }

    //重新计算布局的宽高
    private void setViewSize() {
        int[] tipsSize = CommontUtils.getViewSize(context, R.mipmap.icon_tips2, 20);
        ViewGroup.LayoutParams tipsP = tipsTV.getLayoutParams();
        tipsP.width = tipsSize[0];
        tipsP.height = tipsSize[1];
        tipsP = null;
    }

    private void regEvent(boolean b) {
        if (null != bt_prompt) {
            bt_prompt.setOnClickListener(b ? this : null);
        }
        if (null != bt_prompt_callpolice) {
            bt_prompt_callpolice.setOnClickListener(b ? this : null);
        }
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hn_btn_back:
                closeForm();
                OnDestroy();
                break;
            case R.id.bt_prompt:
                CreateAppCaseNumber();
                break;
            case R.id.bt_prompt_callpolice:
                //报警
                UiUtils.showDialog(getContext(), null, "提示",
                        "请您立即拨打110报警，报警后在现场等待交通警察处理", "取消", "拨打110",
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                checkPhone();
                            }
                        });
                break;
        }
    }

    //打电话
    private void checkPhone() {
        //拨号界面
        Uri uri = Uri.parse("tel:110");
        Intent intent = new Intent(Intent.ACTION_DIAL, uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    // 生成唯一档案编号
    public void CreateAppCaseNumber() {
        try {
            processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
            EntityBean bean = new EntityBean();
            bean.set("areaid", mApplication.getAreaid());
            bean.set("uid", mApplication.getKckpName());
            bean.set("token", mApplication.getToken());
            LoadDataManagerCar.getInstance(context).callService(null,
                    mApplication.getServerUrl(), URLConstant.KCKPCREATEAPPCASENO, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                        @Override
                        public void onSuccess(String key, String service, Object result) {
                            if (null != processDialog) {
                                if (processDialog.isShowing()) {
                                    processDialog.dismiss();
                                }
                            }
                            EntityBean bean = (EntityBean) result;
                            String restate = bean.getString("restate");
                            if ("1".equals(restate)) {
                                EntityBean data = bean.getBean("data");
                                String appCaseNo = data.getString("appcaseno");
                                ChooseAccidentTypeView chooseaccidenttypeview = new ChooseAccidentTypeView(context, appCaseNo);
                                FrameworkManager.getInstance().showNewForm(context, chooseaccidenttypeview);
                            } else {
                                String redes = bean.getString("redes");
                                UiUtil.showToast(context, redes);
                            }
                        }

                        @Override
                        public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                            if (null != processDialog) {
                                if (processDialog.isShowing()) {
                                    processDialog.dismiss();
                                }
                            }
                            if (resultType == LoadDataManagerFather.ResultType.Failure) {
                                UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                            } else if (resultType == LoadDataManagerFather.ResultType.NoNetWorks) {
                                UiUtil.showToast(context, "无网络连接，请检查您的网络");
                            }
                        }

                        @Override
                        public void onFinished(String key, String service) {
                            if (null != processDialog) {
                                if (processDialog.isShowing()) {
                                    processDialog.dismiss();
                                }
                            }
                        }
                    });
        } catch (Exception e) {

            e.printStackTrace();
        }
    }


}
